<template>
  <div class="login-container">
    <el-form class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <h3 class="title">欢迎登陆宝视岛用户管理系统</h3>
      </div>
      <el-from-item>
        <el-tabs v-model="activeName" stretch="true" @tab-click="handleClick" >
          <el-tab-pane label="帐号密码登录" name="first"></el-tab-pane>
          <el-tab-pane label="短信登录" name="second"></el-tab-pane>
        </el-tabs>
      </el-from-item>
      
      <!-- 帐号密码表单 -->
      <el-form id="loginForm" :hidden="upFormHid" ref="loginForm" :model="loginForm" :rules="loginRules">
         <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
        </el-form-item>
        <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
        </el-form-item>
        <el-button :loading="loading1" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>
      </el-form>
     
       <!-- 短信登录表单 -->
      <el-form id="mgsLoginForm" :hidden="mgsFormHid" ref="mgsLoginForm" :model="mgsLoginForm" :rules="mgsLoginRules">
        <el-form-item prop="phone">
          <span class="svg-container">
            <svg-icon icon-class="phone" style="font-size:20px"/>
          </span>
          <el-input
          ref="phone"
          v-model="mgsLoginForm.phone"
          placeholder="手机号码"
          name="phone"
          type="text"
          tabindex="1"
          auto-complete="on"
          />
        </el-form-item>

        <el-form-item prop="verificationNode">
        <span class="svg-container">
          <svg-icon icon-class="message" style="font-size:20px;"/>
        </span>
        <span>
          <el-input
          ref="verificationNode"
          v-model="mgsLoginForm.verificationNode"
          placeholder="验证码"
          name="verificationNode"
          tabindex="2"/>
        </span>
        <span class="show-pwd">
          <el-button type="warning" v-text="countDown" :disabled="getNodeDis" @click.native.prevent="getNode()"></el-button>
        </span>
      </el-form-item>
      <el-button :loading="loading2" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="mgsLogin">登录</el-button>
      </el-form>

    </el-form>
  </div>
</template>

<script>
import {login,getVerificationNode,messageLogin} from '@/api/user'

export default {
  name: 'Login',
  data() {
    return {
      countDown:"获取验证码",//获取验证码按钮显示的内容
      getNodeDis:false,//控制获取验证码按钮禁用属性
      upFormHid:false,//切换登录方式显示控制：账号密码登录
      mgsFormHid:true,////切换登录方式显示控制：短信登录
      loginForm: {
        username: 'xw',
        password: '123456',
        userType:"2"
      },
      mgsLoginForm: {
        phone: '13557533937',
        verificationNode:'267628',
        userType:"2"
      },
      activeName: 'first',
      loginRules: {
        username: [{ required: true, trigger: 'blur' }],
        password: [{ required: true, trigger: 'blur' }]
      },
      mgsLoginRules:{
        phone: [{ required: true, trigger: 'blur' }],
        verificationNode: [{ required: true, trigger: 'blur' }],
      },
      loading1: false,
      loading2: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    //帐号密码登录
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading1 = true;
          var _this = this;
          //异步请求单点登录系统获取jwt令牌
          login(_this.loginForm).then((res) => {
            if(res.success){
              //将jwt令牌存放到sessionStorage
              sessionStorage.setItem("token",res.data);
              //跳转到首页
              _this.$router.push({path:"/"})
            }else{
              _this.$message.error({
                title: '消息提示',
                message: res.msg,
                offset:80,
              });
            }
            _this.loading1 = false;
          }).catch((err) => {
            
          });
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    
    //切换登录方式点击事件
    handleClick() {
      if(this.activeName==='first'){
        this.upFormHid=false;
        this.mgsFormHid=true;
      }else if(this.activeName==='second'){
        this.upFormHid=true;
        this.mgsFormHid=false;
      }
    },
    //获取验证码
    getNode(){
      var _this=this;
      _this.getNodeDis=true;
      for (let i = 60; i >= 0; i--) {
        setTimeout(function(){
            if(i>0){
              _this.countDown= i + ' 秒后重发';
            }else{
              _this.countDown='获取验证码';
              _this.getNodeDis=false;
            }
        },(60-i)*1000);
      };
      getVerificationNode(this.mgsLoginForm).then(res=>{
        
      })
    },
    //短信登录
    mgsLogin(){
      this.$refs.mgsLoginForm.validate(valid => {
        if (valid) {
          this.loading2 = true;
          var _this = this;
          //异步请求单点登录系统获取jwt令牌
          messageLogin(_this.mgsLoginForm).then((res) => {
            if(res.success){
              //将jwt令牌存放到sessionStorage
              sessionStorage.setItem("token",res.data);
              //跳转到首页
              _this.$router.push({path:"/"})
            }else{
              _this.$message.error({
                title: '消息提示',
                message: res.msg,
                offset:80,
              });
            }
            _this.loading2 = false;
          }).catch((err) => {
            
          });
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }


  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
.el-tabs__item {
    padding: 0 20px;
    height: 40px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    line-height: 40px;
    display: inline-block;
    list-style: none;
    font-size: 18px;
    font-weight: 500;
    color: #fefeff;
    position: relative;
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 30px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
